<?php

namespace app\admin\controller;

use app\model\VipSolutionModel;
use app\model\VipDetailModel;
use support\Request;
use support\Response;
use Exception;
use app\model\Util;

class VipSolutionController
{
    /**
     * 获取VIP方案列表
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            
            $query = VipSolutionModel::query()
                ->orderBy('id', 'desc');
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            foreach ($list as &$item) {
                $item['created_at'] = $item->created_at ?: 0;
                $item['updated_at'] = $item->updated_at ?: 0;

                $item['image_url'] = Util::pathToUrl($item->image);

            }
                
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取VIP方案详情
     * @param Request $request
     * @return Response
     */
    public function detail(Request $request): Response
    {
        try {
            $id = $request->get('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $vipSolution = VipSolutionModel::where('id', $id)->first();
                
            if (!$vipSolution) {
                throw new Exception('VIP方案不存在');
            }

            return json([
                'data' => $vipSolution,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 提交/创建VIP方案
     * @param Request $request
     * @return Response
     */
    public function submit(Request $request): Response
    {
        try {
            $data = $request->post();
            
            if (empty($data['name'])) {
                throw new Exception('方案名称不能为空');
            }

            if (empty($data['description'])) {
                throw new Exception('方案描述不能为空');
            }

            if (!isset($data['money']) || $data['money'] < 0) {
                throw new Exception('方案金额不能为空且不能小于0');
            }

            if (empty($data['days']) || $data['days'] < 1) {
                throw new Exception('方案天数不能为空且不能小于1');
            }

            // 判断是新建还是编辑
            if (!empty($data['id'])) {
                $vipSolution = VipSolutionModel::find($data['id']);
                if (!$vipSolution) {
                    throw new Exception('VIP方案不存在');
                }
            } else {
                $vipSolution = new VipSolutionModel();
            }

            $vipSolution->name = $data['name'];
            $vipSolution->description = $data['description'];
            $vipSolution->money = $data['money'];
            $vipSolution->days = $data['days'];
            $vipSolution->image = $data['image'];
            $vipSolution->status = $data['status'] ?? 'online';
            $vipSolution->updated_at = time();
            
            // 新建时设置创建时间
            if (empty($data['id'])) {
                $vipSolution->created_at = time();
            }
            
            if (!$vipSolution->save()) {
                throw new Exception(empty($data['id']) ? '添加失败' : '更新失败');
            }

            return json([
                'data' => $vipSolution,
                'msg' => empty($data['id']) ? '添加成功' : '更新成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除VIP方案
     * @param Request $request
     * @return Response
     */
    public function delete(Request $request): Response
    {
        try {
            $id = $request->post('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $vipSolution = VipSolutionModel::find($id);
            if (!$vipSolution) {
                throw new Exception('VIP方案不存在');
            }

            if (!$vipSolution->delete()) {
                throw new Exception('删除失败');
            }

            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取VIP权益列表
     * @param Request $request
     * @return Response
     */
    public function vipDetailList(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            
            $query = VipDetailModel::query()
                ->orderBy('id', 'desc');
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            foreach ($list as &$item) {
                $item['created_at'] = $item->created_at ?: 0;
                $item['updated_at'] = $item->updated_at ?: 0;

                $item['icon_url'] = Util::pathToUrl($item['icon']);
            }
                
            return json([
                'data' => [
                    'list' => $list,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取VIP权益详情
     * @param Request $request
     * @return Response
     */
    public function vipDetailDetail(Request $request): Response
    {
        try {
            $id = $request->get('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $vipDetail = VipDetailModel::where('id', $id)->first();
                
            if (!$vipDetail) {
                throw new Exception('VIP权益不存在');
            }

            return json([
                'data' => $vipDetail,
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 提交/创建VIP权益
     * @param Request $request
     * @return Response
     */
    public function submitVipDetail(Request $request): Response
    {
        try {
            $data = $request->post();
            
            if (empty($data['name'])) {
                throw new Exception('权益名称不能为空');
            }
            if (empty($data['icon'])) {
                throw new Exception('权益图标不能为空');
            }

            // 判断是新建还是编辑
            if (!empty($data['id'])) {
                $vipDetail = VipDetailModel::find($data['id']);
                if (!$vipDetail) {
                    throw new Exception('VIP权益不存在');
                }
            } else {
                $vipDetail = new VipDetailModel();
            }

            $vipDetail->icon = $data['icon'];
            $vipDetail->name = $data['name'];
            $vipDetail->sub_name = $data['sub_name'] ?? '';
            $vipDetail->description = $data['description'] ?? '';
            $vipDetail->updated_at = time();
            
            // 新建时设置创建时间
            if (empty($data['id'])) {
                $vipDetail->created_at = time();
            }
            
            if (!$vipDetail->save()) {
                throw new Exception(empty($data['id']) ? '添加失败' : '更新失败');
            }

            return json([
                'data' => $vipDetail,
                'msg' => empty($data['id']) ? '添加成功' : '更新成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 删除VIP权益
     * @param Request $request
     * @return Response
     */
    public function deleteVipDetail(Request $request): Response
    {
        try {
            $id = $request->post('id');
            
            if (empty($id)) {
                throw new Exception('参数错误');
            }

            $vipDetail = VipDetailModel::find($id);
            if (!$vipDetail) {
                throw new Exception('VIP权益不存在');
            }

            if (!$vipDetail->delete()) {
                throw new Exception('删除失败');
            }

            return json([
                'data' => null,
                'msg' => '删除成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 